// Ignore argument count for test functions.
#![allow(clippy::too_many_arguments)]

use ndarray::{array, Array2};
use rstest::rstest;

use crate::tests::fixtures::{
    rand_usize,
    random_excluded_connectors,
    random_exposed_edges,
    random_missing_tile_policy,
    random_tile_selection_policy,
    random_tile_text,
    random_tile_type,
    random_vacancy_policy,
    EXPECTED_EXCLUDED_CONNECTORS,
    RANDOM_BOOL,
    RANDOM_CONNECTORS_ALL,
    RANDOM_CONNECTORS_EAST,
    RANDOM_CONNECTORS_EAST_WEST,
    RANDOM_CONNECTORS_NORTH,
    RANDOM_CONNECTORS_SOUTH,
    RANDOM_COORDINATES,
    RANDOM_DIRECTION,
    RANDOM_EXCLUDED_CONNECTORS,
    RANDOM_EXPOSED_EDGES,
    RANDOM_PREDECESSOR,
    RANDOM_TILES,
    RANDOM_USIZE,
    RANDOM_USIZES,
};
use crate::tiling::slot_grid::tests::{
    create_grid_slot_or_none,
    create_grid_slot_with_predecessor,
    create_grid_slot_with_some_tile_id,
    create_reproducible_slot_with_fields,
    pad_coordinates_randomly,
    pad_grid_randomly,
};
use crate::tiling::tiles::tile_list::TileList;
use crate::tiling::tiles::{ArrayInitializable, TileConnectionArray};
use crate::tiling::{Direction, GridSlot, SlotGrid};
use crate::Error;

// noinspection DuplicatedCode
#[rstest]
// region illegal predecessor coordinates, panic
#[should_panic(expected = "ndarray: index (15, 94) is out of bounds for array of shape [2, 6]")]
#[case::illegal_predecessor_coordinates_panic(
    (1, *RANDOM_USIZE),
    Some((15, 94)),
    array![
        [None, None, None, None, None, None],
        [
            None,
            None,
            create_reproducible_slot_with_fields(
                *RANDOM_DIRECTION,
                *RANDOM_PREDECESSOR,
                RANDOM_EXPOSED_EDGES[0].clone(),
            ),
            None,
            None,
            None,
        ],
    ],
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Err(Error::ValueError("inv al".to_string())),
    (1, *RANDOM_USIZE),
    Some((15, 94)),
    array![[]],
    vec![],
)]
// endregion
// region illegal current slot coordinates, panic
#[should_panic(expected = "ndarray: index (1, 7) is out of bounds for array of shape [2, 6]")]
#[case::illegal_current_slot_coordinates_panic(
    (1, 7),
    Some((15, 94)),
    array![
        [None, None, None, None, None, None],
        [
            None,
            None,
            create_reproducible_slot_with_fields(
                *RANDOM_DIRECTION,
                *RANDOM_PREDECESSOR,
                RANDOM_EXPOSED_EDGES[1].clone(),
            ),
            None,
            None,
            None,
        ],
    ],
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Err(Error::ValueError("inv al".to_string())),
    (1, 7),
    Some((15, 94)),
    array![[None]],
    vec![],
)]
// endregion
// region predecessor does not exist, error
#[case::predecessor_does_not_exist_error(
    pad_coordinates_randomly((2, *RANDOM_USIZE)),
    Some(if *RANDOM_BOOL {(1, 3)} else {(2, 1)}),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None, None, None],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    *RANDOM_PREDECESSOR,
                    RANDOM_EXPOSED_EDGES[2].clone(),
                ),
                None,
            ],
            [None, None, None, None, None, None, None, None],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Err(Error::ValueError(format!(
        "current slot (coordinates {:?}) does not exist",
        if *RANDOM_BOOL {(1, 3)} else {(2, 1)},
    ))),
    if *RANDOM_BOOL {(1, 3)} else {(2, 1)},
    Some(if *RANDOM_BOOL {(1, 3)} else {(2, 1)}),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None, None, None],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    *RANDOM_PREDECESSOR,
                    RANDOM_EXPOSED_EDGES[2].clone(),
                ),
                None,
            ],
            [None, None, None, None, None, None, None, None],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
)]
// endregion
// region no predecessor, backtracking failed
#[case::no_predecessor_backtracking_failed(
    pad_coordinates_randomly((1, 2)),
    None,
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    None,
                    RANDOM_EXPOSED_EDGES[3].clone(),
                ),
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Err(
        Error::TileNotFoundException("no more tiles left to try, backtracking failed.".to_string())
    ),
    pad_coordinates_randomly((1, 2)),
    None,
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    None,
                    RANDOM_EXPOSED_EDGES[3].clone(),
                ),
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
)]
// endregion
// region no successors, backtracking successful
#[case::no_successors_backtracking_successful(
    pad_coordinates_randomly((1, 2)),
    Some(pad_coordinates_randomly((1, 1))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[4].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 1))),
                    random_exposed_edges(),
                ),
                None,
                None,
                None,
            ],
            [
                None,
                if *RANDOM_BOOL {
                    None
                } else {
                    Some(GridSlot {
                        tile_id: Some(*RANDOM_USIZE * 4 + 7),
                        direction: *RANDOM_DIRECTION,
                        predecessor: Some(pad_coordinates_randomly((2, 2))),
                        exposed_edges: vec![Direction::South],
                        excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                    })
                },
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Ok(()),
    pad_coordinates_randomly((1, 1)),
    *RANDOM_PREDECESSOR,
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[0].clone(),
                }),
                None,
                None,
                None,
                None,
            ],
            [
                None,
                if *RANDOM_BOOL {
                    None
                } else {
                    Some(GridSlot {
                        tile_id: Some(*RANDOM_USIZE * 4 + 7),
                        direction: *RANDOM_DIRECTION,
                        predecessor: Some(pad_coordinates_randomly((2, 2))),
                        exposed_edges: vec![ Direction::South, Direction::North],
                        excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                    })
                },
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
)]
// endregion
// region no successors, backtracking successful, alternative east
#[case::no_successors_backtracking_successful_alternative_east(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((2, 4))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                None,
                None,
                if *RANDOM_BOOL {
                    None
                } else {
                    Some(GridSlot {
                        tile_id: Some(*RANDOM_USIZE * 4 + 7),
                        direction: *RANDOM_DIRECTION,
                        predecessor: Some(pad_coordinates_randomly((2, 2))),
                        exposed_edges: vec![Direction::East],
                        excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                    })
                },
                None,
            ],
            [
                None,
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((2, 4))),
                    random_exposed_edges(),
                ),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[4].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Ok(()),
    pad_coordinates_randomly((2, 4)),
    *RANDOM_PREDECESSOR,
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                None,
                None,
                if *RANDOM_BOOL {
                    None
                } else {
                    Some(GridSlot {
                        tile_id: Some(*RANDOM_USIZE * 4 + 7),
                        direction: *RANDOM_DIRECTION,
                        predecessor: Some(pad_coordinates_randomly((2, 2))),
                        exposed_edges: vec![Direction::East, Direction::South],
                        excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                    })
                },
                None,
            ],
            [
                None,
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
)]
// endregion
// region no successors, backtracking successful, alternative north
#[case::no_successors_backtracking_successful_alternative_north(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 3))),
                    random_exposed_edges(),
                ),
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Ok(()),
    pad_coordinates_randomly((1, 3)),
    *RANDOM_PREDECESSOR,
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
)]
// endregion
// region successors present, no last leaves, panic
#[should_panic(expected = "there should be last leaves when there is a successor")]
#[case::successors_present_no_last_leaves_panic(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 3))),
                    random_exposed_edges(),
                ),
                None,
                None,
            ],
        ]
    ),
    vec![],
    Ok(()),
    pad_coordinates_randomly((1, 2)),
    Some(pad_coordinates_randomly((1, 3))),
        array![[None]],
    vec![],
)]
// endregion
// region last leaf is successor, backtracking successful
#[case::last_leaf_is_successor_backtracking_successful(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![Direction::North, Direction::East, Direction::South],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                None,
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 3))),
                    random_exposed_edges(),
                ),
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((1, 2)),
    ],
    Ok(()),
    pad_coordinates_randomly((1, 2)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![
                        Direction::North,
                        Direction::East,
                        Direction::South,
                        Direction::West,
                    ],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![(*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3)],
)]
// endregion
// region illegal last leaf coordinates, panic
#[should_panic(expected = "ndarray: index (12, 1) is out of bounds for array of shape [4, 7]")]
#[case::illegal_last_leaf_coordinates_panic(
    (3, 4),
    Some((2, 4)),
    array![
        [None, None, None, None, None, None, None],
        [None, None, None, None, None, None, None],
        [
            None,
            None,
            Some(GridSlot {
                tile_id: Some(*RANDOM_USIZE * 3 + 2),
                direction: *RANDOM_DIRECTION,
                predecessor: Some(pad_coordinates_randomly((2, 3))),
                exposed_edges: vec![Direction::West],
                excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
            }),
            Some(GridSlot {
                tile_id: Some(*RANDOM_USIZE * 3 + 2),
                direction: *RANDOM_DIRECTION,
                predecessor: Some((2, 4)),
                exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
            }),
            Some(GridSlot {
                tile_id: Some(*RANDOM_USIZE * 3 + 1),
                direction: *RANDOM_DIRECTION,
                predecessor: *RANDOM_PREDECESSOR,
                exposed_edges: vec![
                    Direction::North,
                    Direction::East,
                    Direction::South,
                ],
                excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
            }),
            None,
            None
        ],
        [
            None,
            None,
            Some(GridSlot {
                tile_id: Some(*RANDOM_USIZE * 3 + 1),
                direction: *RANDOM_DIRECTION,
                predecessor: None,
                exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
            }),
            None,
            create_reproducible_slot_with_fields(
                *RANDOM_DIRECTION,
                Some((2, 4)),
                RANDOM_EXPOSED_EDGES[5].clone(),
            ),
            None,
            None
        ],
    ],
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        (2, 1),
        (12, 1),
    ],
    Ok(()),
    (12, 1),
    None,
    array![[]],
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        (2, 1),
    ],
)]
// endregion
// region last leaf does not exist, error
#[case::last_leaf_does_not_exist_error(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 2))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![
                        Direction::North,
                        Direction::East,
                        Direction::South,
                    ],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: None,
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 3))),
                    random_exposed_edges(),
                ),
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((2, 1)),
        pad_coordinates_randomly((1, 4)),
    ],
    Err(Error::ValueError(format!(
        "current slot (coordinates {:?}) does not exist",
        pad_coordinates_randomly((1, 4)),
    ))),
    pad_coordinates_randomly((1, 4)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 2))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![
                        Direction::North,
                        Direction::East,
                        Direction::South,
                    ],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: None,
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((2, 1)),
    ],
)]
// endregion
// region last leaf root is thrice_removed, backtracking successful
#[case::last_leaf_root_is_thrice_removed_backtracking_successful(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((1, 3))),
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 2))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![
                        Direction::North,
                        Direction::East,
                        Direction::South,
                    ],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: None,
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 3))),
                    random_exposed_edges(),
                ),
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((2, 1)),
    ],
    Ok(()),
    pad_coordinates_randomly((2, 1)),
    None,
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 2))),
                    exposed_edges: vec![Direction::West, Direction::South],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: vec![
                        Direction::North,
                        Direction::East,
                        Direction::South,
                    ],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: None,
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![(*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3)],
)]
// endregion
// region last leaf is thrice removed, backtracking successful
#[case::last_leaf_is_thrice_removed_backtracking_successful(
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((1, 3))),
    // ╭───────────╮
    // │ E E N     │
    // │ N   X     │
    // ╰───────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 2))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[1].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 1))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 3))),
                    random_exposed_edges(),
                ),
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3),
        pad_coordinates_randomly((2, 1)),
    ],
    Ok(()),
    pad_coordinates_randomly((2, 1)),
    Some(pad_coordinates_randomly((1, 1))),
    // ╭───────────╮
    // │ E E N     │
    // │ ⬚         │
    // ╰───────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 2))),
                    exposed_edges: vec![Direction::West, Direction::South],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[8].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[1].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: Some(pad_coordinates_randomly((1, 1))),
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![(*RANDOM_USIZE + 4, *RANDOM_USIZE * 2 + 3)],
)]
// endregion
// region last leaf is four times removed, delete one leaf, backtracking successful
#[case::last_leaf_is_four_times_removed_delete_one_leaf_backtracking_successful(
    pad_coordinates_randomly((2, 6)),
    Some(pad_coordinates_randomly((1, 6))),
    // ╭─────────────╮
    // │     E E E N │
    // │   6 W 5   X │
    // │     7       │
    // │             │
    // ╰─────────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None, None],
            [
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 4))),
                    exposed_edges: random_exposed_edges(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 5))),
                    exposed_edges: vec![],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 6))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[9].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[1].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
            ],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 6),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 4))),
                    exposed_edges: vec![Direction::North],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[4].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 3),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[5].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 5),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 4))),
                    exposed_edges: vec![],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[6].clone(),
                }),
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((1, 5))),
                    random_exposed_edges(),
                ),
            ],
            [
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 7),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[0].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[0].clone(),
                }),
                None,
                None,
                None,
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        (*RANDOM_USIZE + 5, *RANDOM_USIZE + 2),
        pad_coordinates_randomly((2, 3)),
    ],
    Ok(()),
    pad_coordinates_randomly((1, 3)),
    Some(pad_coordinates_randomly((1, 4))),
    // ╭─────────────╮
    // │     ⬚ E E N │
    // │   6   5     │
    // │     7       │
    // │             │
    // ╰─────────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None, None],
            [
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: Some(pad_coordinates_randomly((1, 4))),
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 5))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 6))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[9].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[1].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
            ],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 6),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 4))),
                    exposed_edges: vec![Direction::North, Direction::East],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[4].clone(),
                }),
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 5),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 4))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[6].clone(),
                }),
                None,
                None,
            ],
            [
                None,
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 7),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[0].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[0].clone(),
                }),
                None,
                None,
                None,
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![(*RANDOM_USIZE + 5, *RANDOM_USIZE + 2)],
)]
// endregion
// region last leaf is four times removed, delete three leaves, backtracking successful
#[case::last_leaf_is_four_times_removed_delete_three_leaves_backtracking_successful(
    pad_coordinates_randomly((3, 6)),
    Some(pad_coordinates_randomly((2, 6))),
    // ╭───────────────╮
    // │   0           │
    // │ 4 A E ? N N   │
    // │ ? A W 5   X   │
    // │   8 7         │
    // │               │
    // ╰───────────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None, None, None],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 10),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 3))),
                    exposed_edges: vec![Direction::East, Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
                None,
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 4),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 1))),
                    exposed_edges: vec![Direction::North],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[9].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 4),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 3))),
                    exposed_edges: random_exposed_edges(),
                    excluded_connectors: random_excluded_connectors(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 2),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 4))),
                    exposed_edges: random_exposed_edges(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 4),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 5))),
                    exposed_edges: vec![],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 6))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[9].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[1].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 5),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 1))),
                    exposed_edges: vec![Direction::South],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 4),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: random_exposed_edges(),
                    excluded_connectors: random_excluded_connectors(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 3),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 3))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[6].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[5].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 5),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 4))),
                    exposed_edges: vec![],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[6].clone(),
                }),
                None,
                create_reproducible_slot_with_fields(
                    *RANDOM_DIRECTION,
                    Some(pad_coordinates_randomly((2, 5))),
                    random_exposed_edges(),
                ),
                None,
            ],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(RANDOM_USIZES[3] / 3),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[0].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 7),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[0].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[0].clone(),
                }),
                None,
                None,
                None,
                None,
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        RANDOM_COORDINATES[0],
        RANDOM_COORDINATES[1],
        pad_coordinates_randomly((3, 2)),
        pad_coordinates_randomly((3, 3)),
    ],
    Ok(()),
    pad_coordinates_randomly((2, 3)),
    Some(pad_coordinates_randomly((2, 4))),
    // ╭───────────────╮
    // │   0           │
    // │ 4   ⬚ ? N N   │
    // │ ?     5       │
    // │   8 7         │
    // │               │
    // ╰───────────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None, None, None],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 10),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 3))),
                    exposed_edges: vec![
                        Direction::East,
                        Direction::West,
                        Direction::South,
                    ],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[1].clone(),
                }),
                None,
                None,
                None,
                None,
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 4),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((1, 1))),
                    exposed_edges: vec![Direction::North, Direction::East],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[9].clone(),
                }),
                None,
                Some(GridSlot {
                    tile_id: None,
                    direction: None,
                    predecessor: Some(pad_coordinates_randomly((2, 4))),
                    exposed_edges: vec![],
                    excluded_connectors: EXPECTED_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 4),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 5))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 6))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[9].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[2].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 3 + 1),
                    direction: *RANDOM_DIRECTION,
                    predecessor: *RANDOM_PREDECESSOR,
                    exposed_edges: RANDOM_EXPOSED_EDGES[1].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
                None,
            ],
            [
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE * 5),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 1))),
                    exposed_edges: vec![Direction::South, Direction::East],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[3].clone(),
                }),
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 5),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 4))),
                    exposed_edges: vec![Direction::West],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[6].clone(),
                }),
                None,
                None,
                None,
            ],
            [
                None,
                None,
                Some(GridSlot {
                    tile_id: Some(RANDOM_USIZES[3] / 3),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: vec![ Direction::West, Direction::North],
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[0].clone(),
                }),
                Some(GridSlot {
                    tile_id: Some(*RANDOM_USIZE + 7),
                    direction: *RANDOM_DIRECTION,
                    predecessor: Some(pad_coordinates_randomly((2, 2))),
                    exposed_edges: RANDOM_EXPOSED_EDGES[0].clone(),
                    excluded_connectors: RANDOM_EXCLUDED_CONNECTORS[0].clone(),
                }),
                None,
                None,
                None,
                None,
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                None,
                None,
            ],
        ]
    ),
    vec![
        RANDOM_COORDINATES[0],
        RANDOM_COORDINATES[1],
    ],
)]
// endregion
// region successors present, initial slot unreachable, panic
#[should_panic(expected = "the current slot should have some predecessor")]
#[case::successors_present_initial_slot_unreachable_panic(
    pad_coordinates_randomly((1, 2)),
    Some(pad_coordinates_randomly((4, 4))),
    // ╭───────────╮
    // │   X       │
    // │   ? ? ? ? │
    // │   ?     ? │
    // │   ?   ? ? │
    // │   ? ? ? ? │
    // ╰───────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                create_grid_slot_or_none(),
                None,
                None,
                None,
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                None,
                create_grid_slot_with_predecessor(None),
                create_grid_slot_with_predecessor(
                    Some((rand_usize(0, 3), rand_usize(0, 3)))
                ),
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_predecessor(
                    Some((rand_usize(0, 3), rand_usize(0, 3)))
                ),
                create_grid_slot_with_predecessor(
                    Some((rand_usize(0, 3), rand_usize(0, 3)))
                ),
            ],
        ]
    ),
    RANDOM_COORDINATES.clone(),
    Ok(()),
    pad_coordinates_randomly((1, 1)),
    *RANDOM_PREDECESSOR,
    array![[]],
    RANDOM_COORDINATES.clone(),
)]
// endregion
// region illegal predecessor’s predecessor’s coordinates, panic
#[should_panic(expected = "ndarray: index (6, 3) is out of bounds for array of shape [6, 6]")]
#[case::illegal_predecessors_predecessors_coordinates_panic(
    (1, 2),
    Some((4, 4)),
    // ╭───────────╮
    // │   X       │
    // │   ? ? ? ? │
    // │   ?     ? │
    // │   ?   ? ? │
    // │   ? ? ? ? │
    // ╰───────────╯
    array![
        [None, None, None, None, None, None],
        [
            None,
            None,
            create_grid_slot_or_none(),
            None,
            None,
            None,
        ],
        [
            None,
            None,
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
        ],
        [
            None,
            None,
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            None,
            None,
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
        ],
        [
            None,
            None,
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            None,
            create_grid_slot_with_predecessor(
                Some((6, 3))
            ),
            create_grid_slot_with_predecessor(
                Some((rand_usize(0, 3), rand_usize(0, 3)))
            ),
        ],
        [
            None,
            None,
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            create_grid_slot_with_predecessor(
                Some((rand_usize(0, 3), rand_usize(0, 3)))
            ),
            create_grid_slot_with_predecessor(
                Some((rand_usize(0, 3), rand_usize(0, 3)))
            ),
        ],
    ],
    RANDOM_COORDINATES.clone(),
    Ok(()),
    pad_coordinates_randomly((1, 1)),
    *RANDOM_PREDECESSOR,
    array![[]],
    RANDOM_COORDINATES.clone(),
)]
// endregion
// region predecessor’s predecessor does not exist, panic
#[should_panic(expected = "the current slot should exist")]
#[case::predecessors_predecessor_does_not_exist_panic(
    pad_coordinates_randomly((1, 2)),
    Some(pad_coordinates_randomly((4, 4))),
    // ╭───────────╮
    // │   X       │
    // │   ? ? ? ? │
    // │   ?     ? │
    // │   ?   ? ? │
    // │   ? ? ? ? │
    // ╰───────────╯
    pad_grid_randomly(
        array![
            [None, None, None, None, None, None],
            [
                None,
                None,
                create_grid_slot_or_none(),
                None,
                None,
                None,
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                None,
                create_grid_slot_with_predecessor(
                    Some(pad_coordinates_randomly((4, 3)))
                ),
                create_grid_slot_with_predecessor(
                    Some((rand_usize(0, 3), rand_usize(0, 3)))
                ),
            ],
            [
                None,
                None,
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_some_tile_id(rand_usize(0, 11)),
                create_grid_slot_with_predecessor(
                    Some((rand_usize(0, 3), rand_usize(0, 3)))
                ),
                create_grid_slot_with_predecessor(
                    Some((rand_usize(0, 3), rand_usize(0, 3)))
                ),
            ],
        ]
    ),
    RANDOM_COORDINATES.clone(),
    Ok(()),
    pad_coordinates_randomly((1, 1)),
    *RANDOM_PREDECESSOR,
    array![[]],
    RANDOM_COORDINATES.clone(),
)]
// endregion
fn test_backtrack(
    #[case] current_slot: (usize, usize),
    #[case] current_predecessor: Option<(usize, usize)>,
    #[case] grid: Array2<Option<GridSlot>>,
    #[case] last_leaves: Vec<(usize, usize)>,
    #[case] expected_result: Result<(), Error>,
    #[case] expected_current_slot: (usize, usize),
    #[case] expected_current_predecessor: Option<(usize, usize)>,
    #[case] expected_grid: Array2<Option<GridSlot>>,
    #[case] expected_last_leaves: Vec<(usize, usize)>,
) {
    let mut tiles = RANDOM_TILES.clone();
    // Set the first predecessor’s tile’s connectors.
    tiles[*RANDOM_USIZE * 2] = random_tile_text(RANDOM_CONNECTORS_SOUTH.clone());
    tiles[*RANDOM_USIZE * 3 + 1] = random_tile_text(RANDOM_CONNECTORS_NORTH.clone());
    tiles[*RANDOM_USIZE * 3 + 2] = random_tile_text(RANDOM_CONNECTORS_EAST.clone());
    tiles[*RANDOM_USIZE * 3 + 3] = random_tile_text(RANDOM_CONNECTORS_EAST_WEST.clone());
    tiles[*RANDOM_USIZE * 3 + 4] = random_tile_text(RANDOM_CONNECTORS_ALL.clone());

    let tile_list = TileList {
        tiles,
        horizontal_connector_names: vec![],
        vertical_connector_names: vec![],
        tile_type: random_tile_type(),
    };
    let mut slot_grid = SlotGrid {
        num_rows: grid.nrows() - 1,
        num_cols: grid.ncols() - 1,
        grid,
        border_tile_id: Some(rand_usize(0, 13)),
        tile_connections: TileConnectionArray::new(&tile_list.tiles),
        tile_list,
        missing_tile_policy: random_missing_tile_policy(),
        tile_selection_policy: random_tile_selection_policy(),
        vacancy_policy: random_vacancy_policy(),
        current_slot,
        current_predecessor,
        last_leaves,
        exposed_edge_count: rand_usize(1, 15),
    };

    let result = slot_grid.backtrack();

    assert_eq!(result, expected_result);
    assert_eq!(slot_grid.current_slot, expected_current_slot);
    assert_eq!(slot_grid.current_predecessor, expected_current_predecessor);
    assert_eq!(slot_grid.grid, expected_grid);
    assert_eq!(slot_grid.last_leaves, expected_last_leaves);
}
